<?php
/**
 * Created by PhpStorm.
 * Date: 2021/11/19
 * Time: 15:10
 */

namespace ApiSdk\SmallRedBook\Servers\Request;


class SmallRedBookBaseRequest
{
    public $param = [];
    //应用请求地址
    public $url;
    //应用授权地址
    public $authUrl;
    //应用key
    public $appId;
    //应用密钥
    public $appSecret;
    //跳转地址
    public $redirectUri;

    //版本
    public $version = '2.0';
    //请求时间
    public $timestamp;
    //响应结果
    private $response;

    public $accessToken = '';

    public function __construct()
    {
        $this->authUrl  =config("platform.SmallRedBook.smallredbook_auth_url");
        $this->url      =config("platform.SmallRedBook.smallredbook_request_url");
        $this->appId    =config("platform.SmallRedBook.smallredbook_app_id");
        $this->appSecret=config("platform.SmallRedBook.smallredbook_app_secret");
        $this->redirectUri=config("platform.SmallRedBook.smallredbook_redirect_uri");
        $this->timestamp = time();
    }

    /**
     * 设置访问令牌
     * author: Linqh
     * Date  2021/11/20 14:38
     * @param string $accessToken
     */
    public function setAccessToken(string $accessToken)
    {
        $this->accessToken = $accessToken;
    }

    /**
     * 添加单个参数
     * author: Linqh
     * Date  2021/11/19 14:19
     * @param string $key
     * @param $value
     */
    public function addParam(string $key, $value)
    {
        $this->param[$key] = $value;
    }

    /**
     * 添加参数值
     * author: Linqh
     * Date  2021/11/19 14:19
     * @param array $param
     */
    public function addParams(array $param)
    {
        if (!empty($param)) {
            foreach ($param as $paramKey => $apiParam) {
                if (is_object($apiParam) || is_array($apiParam)) {
                    $apiParam = $apiParam;
                }
                $this->addParam($paramKey, $apiParam);
            }
        }
    }

    /**
     * GET请求
     * author: Linqh
     * Date  2021/11/19 14:34
     * @return mixed
     */
    public function get()
    {
        if ($this->param) {
            $url = $this->url . '?' . http_build_query($this->param);
        }
        $this->response = $this->httpsRequest($url);
        $result         = json_decode($this->response, true);
        return $result;
    }

    /**
     * POST请求
     * author: Linqh
     * Date  2021/11/19 14:34
     * @return mixed
     */
    public function post()
    {
        $header         = [
            'Accept:application/json', 'Content-Type:application/json'
        ];
        $url            = $this->url;
        $this->response = $this->httpsRequest($url, json_encode($this->param), $header);
        $result         = json_decode($this->response, true);
        return $result;
    }

    /**
     * 请求
     * author: Linqh
     * Date  2021/11/20 14:11
     * @param $url
     * @param null $data
     * @param null $headers
     * @return bool|string
     */
    public function httpsRequest($url, $data = null, $headers = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        if (!empty($headers)) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }
        //设置curl默认访问为IPv4
        if (defined('CURLOPT_IPRESOLVE') && defined('CURL_IPRESOLVE_V4')) {
            curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        }
        $output = curl_exec($curl);
        curl_close($curl);
        return ($output);
    }

    /**
     * 设置公共参数
     * author: Linqh
     * Date  2021/11/20 17:12
     * @param $method
     */
    public function setCommonParam($method)
    {
        $this->addParam('method', $method);
        $this->addParam('appId', $this->appId);
        $this->addParam('sign', $this->sign($method));
        $this->addParam('timestamp', $this->timestamp);
        $this->addParam('version', $this->version);
        $this->addParam('accessToken', $this->accessToken);
    }


    /**
     * 获取签名
     * author: Linqh
     * Date  2021/11/20 13:50
     * @param $action
     * @return string
     */
    public function sign($action)
    {
        $param = [
            'appId'     => $this->appId,
            'timestamp' => $this->timestamp,
            'version'   => $this->version,
        ];
        return md5($action . '?' . http_build_query($param) . $this->appSecret);
    }

}
